package com.example.sjly.ui.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sjly.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

//RecyclerView的适配器
class OrderListAdapter extends RecyclerView.Adapter<OrderListAdapter.OrderListViewHolder> {


    private List<Order> mOrderList = new ArrayList<>();//数据队列
    private Context mContext;

    public OrderListAdapter(Context mContext, List<Order> mOrderList) {
        this.mOrderList = mOrderList;
        this.mContext = mContext;
    }

    @NonNull
    @NotNull
    @Override
    public OrderListViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {//创建适配器item
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_item_order, parent, false);
        OrderListViewHolder holder = new OrderListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull OrderListViewHolder holder, int position) {//对item注册事件
        holder.mDeleteBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OrderRoomDatabase.getInstance(mContext).mDAO().delete(mOrderList.get(position));
                mOrderList.remove(position);
                notifyDataSetChanged();
            }
        });
        Order order=mOrderList.get(position);
        holder.mComNameTV.setText(order.getCommodityName());
        holder.mTypeNameTV.setText(order.getTypeName());
    }

    @Override
    public int getItemCount() {//获取队列长度
        return mOrderList.size();
    }

    class OrderListViewHolder extends RecyclerView.ViewHolder {//item对象

        Button mDeleteBT;
        TextView mTypeNameTV;
        TextView mComNameTV;


        public OrderListViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            mDeleteBT=itemView.findViewById(R.id.order_delete_bt);
            mTypeNameTV=itemView.findViewById(R.id.type_name_tv);
            mComNameTV=itemView.findViewById(R.id.commodity_name_tv);
        }
    }

}
